package com.system.controller;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

@Service
public class EmailTimeTask implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        //获取工厂
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        //调度器
        Scheduler scheduler = schedulerFactory.getScheduler();

        //定时任务A
        JobKey jobKeyA = new JobKey("jobA","groupA");
        JobDetail jobDetailA = JobBuilder.newJob(JobA.class)
                .withIdentity(jobKeyA)
                .build();

        //定义触发的条件
        //每个10秒触发一次
        Trigger triggerA = TriggerBuilder
                .newTrigger()
                .withIdentity("dummyTriggerNameA","groupA")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
                .build();

        //4. 启动定时任务
        scheduler.start();
        scheduler.scheduleJob(jobDetailA,triggerA);
    }
}
